package com.hna.eking.AirGisPlt.Rest;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.mybatis.spring.MyBatisSystemException;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hna.eking.AirGisPlt.Mapper.MapStarFeatureMapper;
import com.hna.eking.AirGisPlt.Mapper.MapStarInfoMapper;
import com.hna.eking.AirGisPlt.Mapper.WetherStationMapper;
import com.hna.eking.AirGisPlt.MybatisImp.AirportInfo;
import com.hna.eking.AirGisPlt.MybatisImp.DEMInfo;
import com.hna.eking.AirGisPlt.MybatisImp.MapStarFeature;
import com.hna.eking.AirGisPlt.MybatisImp.MapStarInfo;
import com.hna.eking.AirGisPlt.MybatisImp.WetherStationInfo;
import com.hna.eking.AirGisPlt.Utils.GetAreaWetherUtils;
import java.sql.Date;
@RestController
@MapperScan({"com.hna.eking.AirGisPlt.Mapper", "com.hna.eking.AirGisPlt.MybatisImp"})
public class WetherStationRest {
	@Autowired
	WetherStationInfo WetherStation;
	
	@Autowired
	DEMInfo DEMInfo;
	
	@Autowired
	MapStarFeature MapStarFeatureInfo;
	
	@Autowired
	MapStarInfo MapStarInfo;
	
	double DEMValue;
	List<WetherStationMapper> WetherStationRet;
	private static final Logger logger = LoggerFactory.getLogger(WetherStationRest.class);
	
	@RequestMapping(value = "/TestFeat", method = RequestMethod.POST)
	@ResponseBody
	public String PostFtTest(HttpServletRequest request) {
		JSONObject json = new JSONObject();
		MapStarFeatureMapper mapFtinfo = new MapStarFeatureMapper();
		mapFtinfo.setAirportCode(request.getParameter("AirportCode"));
		mapFtinfo.setFeatType(request.getParameter("FeatType"));
		mapFtinfo.setAngle(request.getParameter("Angle"));
		mapFtinfo.setLength(request.getParameter("Length"));
		mapFtinfo.setMaxSpeed(request.getParameter("MaxSpeed"));
		mapFtinfo.setElev(request.getParameter("Elev"));	
		mapFtinfo.setAist(request.getParameter("ATIS"));
		mapFtinfo.setAistCode(request.getParameter("AistCode"));
		mapFtinfo.setLineGeo(request.getParameter("LineGeo"));
		
		MapStarFeatureInfo.InsertMapStarInfo(mapFtinfo);
		
		return "success";
	}
	
	@RequestMapping(value = "/TestFeat", method = RequestMethod.GET)
	@ResponseBody
	public String GetFtTest(HttpServletRequest request) {
		String AirLineCode = request.getParameter("AirportCode");
		
		List<MapStarFeatureMapper> MapStarFeature;
		
		JSONArray temp = new JSONArray();
		
		MapStarFeature = MapStarFeatureInfo.getByAirportCode(AirLineCode);
		for(int i = 0; i < MapStarFeature.size(); i++){
			JSONObject json = new JSONObject();
			json.put("AirportCode", MapStarFeature.get(i).getAirportCode());
			json.put("FeatType", MapStarFeature.get(i).getFeatType());
			json.put("StartFlag", MapStarFeature.get(i).getStartFlag());
			json.put("Angle", MapStarFeature.get(i).getAngle());
			json.put("MaxSpeed", MapStarFeature.get(i).getMaxSpeed());
			json.put("Elev", MapStarFeature.get(i).getElev());
			json.put("Aist", MapStarFeature.get(i).getAist());
			json.put("AistCode", MapStarFeature.get(i).getAistCode());
			json.put("LineGeo", MapStarFeature.get(i).getLineGeo());
			logger.debug("test:" + json.toJSONString());
			temp.add(i, json);
			
		}
		JSONObject Ret = new JSONObject();
		Ret.put("FeatArry", temp);
		logger.debug("result:" + Ret.toJSONString());
		return Ret.toJSONString();
	}
	@RequestMapping(value = "/MainInfo", method = RequestMethod.POST)
	@ResponseBody
	public String PostTest(HttpServletRequest request) {
		logger.debug("Insert MainInfo===============>" );
		JSONObject json = new JSONObject();
		MapStarInfoMapper mapstarinfo = new MapStarInfoMapper();
		mapstarinfo.setAirportCode(request.getParameter("AirportCode"));
		mapstarinfo.setPubDate(request.getParameter("PubDate"));
		mapstarinfo.setIndCode(request.getParameter("IndCode"));
		mapstarinfo.setAirportName(request.getParameter("AirportName"));
		mapstarinfo.setCountry(request.getParameter("Country"));
		mapstarinfo.setCity(request.getParameter("City"));
		
		mapstarinfo.setTransLevSet(request.getParameter("TransLevSet"));
		mapstarinfo.setTransALT(request.getParameter("TransALT"));
		mapstarinfo.setATIS(request.getParameter("ATIS"));
		mapstarinfo.setAptElev(request.getParameter("AptElev"));
		mapstarinfo.setTransLev(request.getParameter("TransLev"));
		mapstarinfo.setMaxSpeed(request.getParameter("MaxSpeed"));
		mapstarinfo.setFLConv(request.getParameter("FLConv"));
		mapstarinfo.setFTConv(request.getParameter("FTConv"));
		mapstarinfo.setArrvCode(request.getParameter("ArrvCode"));
		mapstarinfo.setRunway(request.getParameter("Runway"));
		logger.debug("Insert column:" + mapstarinfo.getAirportName());
		MapStarInfo.InsertMapStarInfo(mapstarinfo);
		
		return "success";
	}
	
	@RequestMapping(value = "/MainInfo", method = RequestMethod.GET)
	@ResponseBody
	public String GetTest(HttpServletRequest request){
		JSONObject json = new JSONObject();
		String AirLineCode = request.getParameter("AirportCode");
		
		List<MapStarInfoMapper> mapstarinfo;
		mapstarinfo = MapStarInfo.getByAirportCode(AirLineCode);
		
		json.put("AirportCode", mapstarinfo.get(0).getAirportCode());
		json.put("PubDate", mapstarinfo.get(0).getPubDate());
		json.put("IndCode", mapstarinfo.get(0).getIndCode());
		json.put("AirportName", mapstarinfo.get(0).getAirportName());
		json.put("Country", mapstarinfo.get(0).getCountry());
		json.put("City", mapstarinfo.get(0).getCity());
		json.put("TransLevSet", mapstarinfo.get(0).getTransLevSet());
		json.put("TransALT", mapstarinfo.get(0).getTransALT());
		json.put("ATIS", mapstarinfo.get(0).getATIS());
		json.put("AptElev", mapstarinfo.get(0).getAptElev());
		json.put("TransLev", mapstarinfo.get(0).getTransLev());
		json.put("MaxSpeed", mapstarinfo.get(0).getMaxSpeed());
		json.put("FLConv", mapstarinfo.get(0).getFLConv());
		json.put("FTConv", mapstarinfo.get(0).getFTConv());
		json.put("ArrvCode", mapstarinfo.get(0).getArrvCode());
		json.put("Runway", mapstarinfo.get(0).getRunway());

		return json.toJSONString();
	}
	
	@RequestMapping(value = "/NerStation", method = RequestMethod.GET)
	@ResponseBody
	public String GetAirportPos(@RequestParam(value = "PosX") String PosX, @RequestParam(value = "PosY") String PosY) {
		JSONObject json = new JSONObject();
		
		/*查找最近气象观测站*/
		String Pos = String.format("Point(%s %s)", PosX, PosY);
		WetherStationRet = WetherStation.getByStationCode(Pos);
		
		/*如果附近多个观测站距离相同, 取数据库中第一个*/
		String AreaCode = String.format("%d", WetherStationRet.get(0).getAreacode());
		String StationName = WetherStationRet.get(0).getStation();
		GetAreaWetherUtils WetherInfo = new GetAreaWetherUtils();
		String Repos = WetherInfo.GetWetherInfo(Double.parseDouble(PosX), Double.parseDouble(PosY));
		//json = JSONObject.parseObject(Repos);
		logger.info(Repos);
		logger.info("result: " + WetherStationRet.get(0).getAreacode() + "---" +  WetherStationRet.get(0).getStation());
		return Repos;
	}
	
	@RequestMapping(value = "/DEMInfo", method = RequestMethod.GET)
	@ResponseBody
	public String GetDemInfo(@RequestParam(value = "PosX") String PosX, @RequestParam(value = "PosY") String PosY) {
		JSONObject json = new JSONObject();
		
		/*查找最近气象观测站*/
		String Pos = String.format("Point(%s %s)", PosX, PosY);
		logger.info("debug:" + Pos);
		DEMValue = DEMInfo.getDEMInfo(Pos);
		
		
		logger.info("result:" + DEMValue);
	
		return String.format("%f", DEMValue);
	}
}
